/**
 * 公共接口
 */
import { baseServices } from '@/api';
import YouXin from 'youxin-js-sdk';
const state = {
    OSSDomain: '',
    Platform: ''
  },
  mutationTypes = {
    SET_USER: 'setUser' // 推荐
  },
  actionTypes = {
    SET_RECOMMEND: 'setRecommend', // 推荐
    SET_UPLOADFILE: 'setUploadFileAction', // 上传
    SET_PLATFORM: 'setPlatform' // 获取上传域名和平台标识
  },
  mutations = {
    [mutationTypes.SET_USER] (state, payload = {}) {
      Object.keys(state).forEach((key) => {
        payload[key] !== undefined && (state[key] = (payload || {})[key]);
      });
      sessionStorage.setItem('loginData', JSON.stringify(state));
      YouXin.config({
        origin: state.OSSDomain,
        infoData: {
          app: 'rms',
          areacode: 'nrms'
        }
      });
    }
  },
  actions = {
    // 获取上传域名和平台标识
    async [actionTypes.SET_PLATFORM] ({ commit, state }, param) {
      const [{ data: OSSDomain = {} }, { data: flatform = {} }] = await Promise.all([
        baseServices.getDomain('OSS'),
        baseServices.getDomain('PLATFORM_ABB')
      ]);
      commit(mutationTypes.SET_USER, {
        OSSDomain: `${OSSDomain.protocal}://${OSSDomain.host}`,
        Platform: flatform.host
      });
      return { id: 1 };
    }
  };

export default {
  state,
  mutations,
  actions
};
